package es.uji.viselab.benchmark.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;

import es.uji.viselab.benchmark.DataPackage;
import es.uji.viselab.benchmark.DataType;
import es.uji.viselab.experiment.ExperimentParameters;
import es.uji.viselab.util.LogLevel;
import es.uji.viselab.util.LogRecorder;
import es.uji.viselab.util.Util;

public class DBNonPersistent extends DBConnection {

	List<DataPackage> list = null;
	private static int sequence;

	public DBNonPersistent() {
		sequence = 0;
		list = new ArrayList<DataPackage>();
	}

	@Override
	public void insert(DataPackage dp) {
		sequence = sequence + 1;
		LogRecorder.log(LogLevel.STATS,
				"New record in database, seq: " + sequence + ":" + dp.print());
		dp.setExperimentId(experimentId);
		dp.setGroupId(groupId);
		list.add(dp);
	}

	@Override
	public void Connect() {
	}

	@Override
	public double[][] getData(DataType dt) {
		Iterator<DataPackage> iterator = list.iterator();
		int elements = 0;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt))
				elements++;
		}
		double[][] result = new double[elements][dt.getNumElements()];
		int index = 0;
		int numValues = dt.getNumElements();
		iterator = list.iterator();
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt)) {
				for (int i = 0; i < numValues; i++)
					result[index][i] = dp.getValue(i + 1);
				index++;
			}
		}
		// iterator = list.iterator();
		return result;
	}

	@Override
	public JPanel getPanel() {
		return null;
	}

	@Override
	public String getPanelTitle() {
		return null;
	}

	@Override
	public int panelWidth() {
		return 0;
	}

	@Override
	public int panelHeight() {
		return 0;
	}

	@Override
	public double getIndicator(int experimentId, DataType dt) {
		switch (dt) {
		case FINAL_ERROR:
			return finalError(experimentId);
		case TOTAL_STEPS:
			return totalSteps(experimentId);
		}
		return 0;
	}

	private double totalSteps(int experimentId) {
		Iterator<DataPackage> iterator = list.iterator();
		double count = 0;
		DataType dt = DataType.ERROR;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt)&& dp.getExperimentId()==experimentId)
				count++;
		}
		return count;
	}

	private double finalError(int experimentId) {
		Iterator<DataPackage> iterator = list.iterator();
		double error = 99999;
		DataType dt = DataType.ERROR;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt) && dp.getExperimentId()==experimentId)
				error = Util.format(dp.getValue(1),2);
		}
		return error;
	}

	@Override
	public int nextExperimentId() {
		int newId = lastExperimentId()+1;
		experimentId=newId;
		return experimentId;
	}

	private int lastExperimentId() {
		Iterator<DataPackage> iterator = list.iterator();
		int id = 0;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.getExperimentId()>id)
				id=dp.getExperimentId();
		}
		return id;
	}

	@Override
	public int nextGroupId() {
		int newId = lastGrouptId()+1;
		experimentId=newId;
		return experimentId;
	}

	private int lastGrouptId() {
		Iterator<DataPackage> iterator = list.iterator();
		int id = 0;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.getGroupId()>id)
				id=dp.getGroupId();
		}
		return id;
	}

	@Override
	public List<Integer> getExperimentsList(int groupId) {
		List<Integer> experimentList = new ArrayList<Integer>();
		
		Iterator<DataPackage> iterator = list.iterator();
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.getGroupId()==groupId){
				experimentId=dp.getExperimentId();
				if (!experimentList.contains(experimentId))
					experimentList.add(experimentId);
			}
		}
		return experimentList;
	}

	@Override
	public ExperimentParameters getExperimentParameters(int experimentId) {
		Iterator<DataPackage> iterator = list.iterator();
		String parameters=null;
		ExperimentParameters ep = null;
		DataType dt = DataType.HEADER;
		while (iterator.hasNext()) {
			DataPackage dp = iterator.next();
			if (dp.equalsType(dt) && dp.getExperimentId()==experimentId)
				parameters = dp.getText();
		}
		if (parameters!=null)
		  ep = ExperimentParameters.stringToExperimentParameters(parameters);
				  
		return ep;
	}

	
}
